<?php

/**
* LocalizationTest.php
*
* Test localization
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';
require_once '../framework/AuthSingleton.php';

class  LocalizationTest extends WebTestCase
{

    private $url;


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";
        $this->msg = md5( uniqid( rand(), true ) );

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = rtrim($global['service_url'],'/');

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $this->dbh = new PDO($options['dsn_string']); 
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");

        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        $this->setCookie('locale','');

    }

    public function testSetDefaultHttpAcceptLanguageDontExist()
    {
        $this->get($this->url."?cmd=CodeList");
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        $this->assertText('pl_PL');
        $this->assertLink('en_GB',$this->url.'/?cmd=CodeList&locale=en_GB');

    }

    public function testSetDefaultHttpAcceptLanguageExist1()
    {
        $this->addHeader("Accept-Language: en-GB,en;q=0.7,chrome://global/locale/intl.properties;q=0.3");
        $this->get($this->url.'/?cmd=CodeList');
        $this->assertText('Overview and list of trophy');
        $this->assertText('en_GB');
        $this->assertLink('pl_PL',$this->url.'/?cmd=CodeList&locale=pl_PL');

    }

    public function testSetDefaultHttpAcceptLanguageExist2()
    {
        $this->addHeader("Accept-Language: en;q=0.7,chrome://global/locale/intl.properties;q=0.3");
        $this->get($this->url.'/?cmd=CodeList');
        $this->assertText('Overview and list of trophy');
        $this->assertText('en_GB');
        $this->assertLink('pl_PL',$this->url.'/?cmd=CodeList&locale=pl_PL');
    }

    public function testSetDefaultHttpAcceptLanguageExist3()
    {
        $this->addHeader("Accept-Language: en_US;q=0.7,chrome://global/locale/intl.properties;q=0.3");
        $this->get($this->url.'?cmd=CodeList');
        $this->assertText('Overview and list of trophy');
        $this->assertText('en_GB');
        $this->assertLink('pl_PL',$this->url.'/?cmd=CodeList&locale=pl_PL');
    }

    public function testChangeDefault()
    {
        $this->addHeader("Accept-Language: en;q=0.7,chrome://global/locale/intl.properties;q=0.3");
        $this->get($this->url.'?cmd=CodeList');
        $this->assertText('Overview and list of trophy');
        $this->assertText('en_GB');
        $this->assertLink('pl_PL',$this->url.'/?cmd=CodeList&locale=pl_PL');
        $this->click('pl_PL');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        $this->assertText('pl_PL');
        $this->assertLink('en_GB',$this->url.'/?cmd=CodeList&locale=en_GB');
    }


    public function testAllLocaleAvaliable()
    {
        $this->get($this->url.'?cmd=CodeList');
        $options = &PEAR::getStaticProperty('CONFIG_INI','LOCALE');
        $this->assertTrue(is_dir($options['locale_dir']));
 
        $dh = opendir($options['locale_dir']);

        while (($file = readdir($dh)) !== false) 
        {
            if ( $file !="." && $file !=".."  && !strstr($file,'.'))
            {
                if ( $file == $options['default_locale'] ) 
                {
                    $this->assertText($file);
                }
                else
                {
                    $this->assertLink($file,$this->url.'/?cmd=CodeList&locale='.$file);
                }
            }
        }

    }

    public function testDefaultLanguageFollow()
    {
        $this->addHeader("Accept-Language: en-GB,en;q=0.7,chrome://global/locale/intl.properties;q=0.3");
        $this->get($this->url.'?cmd=CodeList');
        $this->assertText('Overview and list of trophy');
        $this->assertText('en_GB');
        $this->assertLink('pl_PL',$this->url.'/?cmd=CodeList&locale=pl_PL');
        $this->addHeader("Accept-Language: en-GB,en;q=0.7,chrome://global/locale/intl.properties;q=0.3");
        $this->get($this->url.'?cmd=CodeList');
        $this->assertText('Overview and list of trophy');
        $this->assertText('en_GB');
        $this->assertLink('pl_PL',$this->url.'/?cmd=CodeList&locale=pl_PL');
    }

    public function testSelectedLanguageFollow()
    {
        $this->addHeader("Accept-Language: en;q=0.7,chrome://global/locale/intl.properties;q=0.3");
        $this->get($this->url.'?cmd=CodeList');
        $this->assertText('Overview and list of trophy');
        $this->assertText('en_GB');
        $this->assertLink('pl_PL',$this->url.'/?cmd=CodeList&locale=pl_PL');
        $this->click('pl_PL');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        $this->assertText('pl_PL');
        $this->assertLink('en_GB',$this->url.'/?cmd=CodeList&locale=en_GB');
        $this->get($this->url.'?cmd=CodeList');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        $this->assertText('pl_PL');
        $this->assertLink('en_GB',$this->url.'/?cmd=CodeList&locale=en_GB');
    }

    public function tearDown() 
    {

    }
 

}
?>
